package com.example.weather.girl

import android.content.Context
import androidx.hilt.Assisted
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.example.library_base.base.BaseViewModel
import com.example.library_base.util.LogUtil
import com.example.weather.R
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GirlViewModel @Inject constructor(
    private val repository: GirlRepository,
    @Assisted private val savedStateHandle: SavedStateHandle
): BaseViewModel<GirlModel>() {

    companion object{
        const val TAG = "TodayWeatherViewModel"
    }

    @Inject
    lateinit var girlAdapter: GirlAdapter

    //查询美女
    fun getGirl(context: Context){
        val map = mutableMapOf<String, Any>()
        map["num"] = 10

        viewModelScope.launch {

//            //并行执行
//            val result1 = async {
//                return@async repository.executionSequence1(map)
//            }
//            val result2 = async {
//                return@async repository.executionSequence2(map)
//            }
//            val result3 = async {
//                return@async repository.executionSequence3(map)
//            }
//            LogUtil.debug(TAG, "返回结果 result1: ${result1.await()}")
//            LogUtil.debug(TAG, "返回结果 result2: ${result2.await()}")
//            LogUtil.debug(TAG, "返回结果 result3: ${result3.await()}")

//            //串行执行
//            val result1 = repository.executionSequence1(map)
//            LogUtil.debug(TAG, "返回结果 result1: $result1")
//
//            val result2 = repository.executionSequence2(map)
//            LogUtil.debug(TAG, "返回结果 result2: $result2")
//
//            val result3 = repository.executionSequence3(map)
//            LogUtil.debug(TAG, "返回结果 result3: $result3")
//
//            LogUtil.debug(TAG, "返回结果launch：\n $result1 \n $result2 \n $result3")

//            val result = repository.getGirlList(map)
//            result?.let {
//                LogUtil.debug(TAG, "返回结果：${it}")
//                val girls = TransformUtil.transAnyToList(it, GirlModel::class.java)
//                LogUtil.debug(TAG, "转换后结果 $girls")
//                girlAdapter.data.addAll(girls)
//            }

            val gson = Gson()
            val json = "[{\"imgurl\":\"http://img5.adesk.com/5fee62b40422087bb13b5c28\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/5fffdfd631f6133d317ee946\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/6073f80ce7bce775811a34aa\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/6022a281e7bce75a5207338f\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/605455c5e7bce72d8e2dc7c4\", \"type\":\"right\", \"des\": \"Right 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/5fee625ac8cfb778f2b446c4\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/5fffdfabe7bce752fca37892\", \"type\":\"right\", \"des\": \"Right 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/604b36f6e7bce735f4129f7f\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/6066f5bde7bce775adc5ae19\", \"type\":\"left\", \"des\": \"Left 显示内容位置\"}," +
                    "{\"imgurl\":\"http://img5.adesk.com/5fffdf24e7bce77ec969e37c\", \"type\":\"right\", \"des\": \"Right 显示内容位置\"}" +
                    "]"
            val girls = gson.fromJson<List<GirlModel>>(json, object: TypeToken<List<GirlModel>>(){}.type)

            LogUtil.debug(TAG, girls.toString())

            val headerModel = GirlHeaderModel()
            headerModel.txt = "这是标题头"

            val headerModel1 = GirlHeaderModel()
            headerModel1.txt = "这是标题头1111"

            val footerModel = GirlFooterModel()
            footerModel.footer = "这是尾部内容"

            val footerModel1 = GirlFooterModel()
            footerModel1.footer = "这是尾部内容111111"

            girlAdapter.mData.addAll(girls)

            girlAdapter.addHeaderView(R.layout.item_girl_header, headerModel)

            girlAdapter.addFooterView(R.layout.item_girl_footer, footerModel)

            girlAdapter.addHeaderView(R.layout.item_girl_header_1, headerModel1)

            girlAdapter.addFooterView(R.layout.item_girl_footer_1, footerModel1)

        }
    }

}